﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace SIOGDE.Negocios.Utils
{
    public class EmailSender
    {
        /*
        public static void SendObsolete(string htmlCont, List<string> destinos)
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("sipre.ejercito@hotmail.com", "SIPRE - BOLIVIA");

            foreach(var item in destinos)
               mailMessage.To.Add(new MailAddress(item));
            mailMessage.Subject = "SIPRE - No Responder";
            mailMessage.Body = htmlCont;
            mailMessage.IsBodyHtml = true;

            SmtpClient smtpClient = new SmtpClient("smtp.live.com", 587);
            //SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            //smtpClient.Credentials = new System.Net.NetworkCredential("sipre.ejercito@gmail.com", "ejercito.sipre");
            smtpClient.Credentials = new System.Net.NetworkCredential("sipre.ejercito@hotmail.com", "1qaz2wsx$");
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

            smtpClient.Send(mailMessage);

            //Didsoft.Utils.Mail.Sender.SendMail(Didsoft.Utils.Mail.SmtpSetting.GetGmailSetting(), email, "SIPRE - No Responder", htmlContent);
        }*/

        /// <summary>
        /// Sends Email with thread
        /// </summary>
        /// <param name="addresses"></param>
        /// <param name="subject"></param>
        /// <param name="message"></param>

        public static void SendEmail(List<string> addresses, string subject, string message)
        {
            System.Threading.Thread threadSendMails;
            threadSendMails = new System.Threading.Thread(delegate()
            {

                /*string email = "jhoncacru@gmail.com";
                string password = "bdhe7612$";

                var loginInfo = new System.Net.NetworkCredential(email, password);
                var msg = new MailMessage();
                var smtpClient = new SmtpClient("smtp.gmail.com", 587);

                msg.From = new MailAddress(email, "SIPRADE");
                foreach (var item in addresses)
                    msg.To.Add(new MailAddress(item));
                //msg.To.Add(new MailAddress(addresses));
                msg.Subject = subject;
                msg.Body = message;
                msg.IsBodyHtml = true;

                smtpClient.EnableSsl = true;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = loginInfo;
                smtpClient.Send(msg);*/

                SendEmailAlone(addresses, subject, message);
            });
            threadSendMails.IsBackground = true;
            threadSendMails.Start();
        }

        /*public static void SendEmailWithoutThread(List<string> addresses, string subject, string message)
        {
            

                string email = "jhoncacru@gmail.com";
                string password = "bdhe7612$";

                var loginInfo = new System.Net.NetworkCredential(email, password);
                var msg = new MailMessage();
                var smtpClient = new SmtpClient("smtp.gmail.com", 587);

                msg.From = new MailAddress(email, "SIPRADE");
                foreach (var item in addresses)
                    msg.To.Add(new MailAddress(item));
                //msg.To.Add(new MailAddress(addresses));
                msg.Subject = subject;
                msg.Body = message;
                msg.IsBodyHtml = true;

                smtpClient.EnableSsl = true;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = loginInfo;
                smtpClient.Send(msg);

            
        }*/

        public static void SendEmailAlone(List<string> addresses, string subject, string message)
        {
            var config = SIOGDE.Negocios.Servicios.BusinessFactory.Current.GetSmtpConfigNegocios().ObtenerTodas().FirstOrDefault();

            var mailMessage = new System.Net.Mail.MailMessage(config.AdministratorEmail, addresses[0]);
            mailMessage.Subject = subject;
            mailMessage.Body = message;
            mailMessage.IsBodyHtml = true;
            GetSmtpClient().Send(mailMessage);
        }

        public static System.Net.Mail.SmtpClient GetSmtpClient()
        {
            var config = SIOGDE.Negocios.Servicios.BusinessFactory.Current.GetSmtpConfigNegocios().ObtenerTodas().FirstOrDefault();


            var smtpClient = new System.Net.Mail.SmtpClient();
            smtpClient.Host = config.HostName;
            smtpClient.Credentials = new System.Net.NetworkCredential(config.DefaultUserName , config.DefaultUserPassword);
            smtpClient.EnableSsl = config.IsSSLEnabled;
            return smtpClient;
        }



        internal static void SendTestEmail()
        {
            var config = SIOGDE.Negocios.Servicios.BusinessFactory.Current.GetSmtpConfigNegocios().ObtenerTodas().FirstOrDefault();
            var mailMessage = new System.Net.Mail.MailMessage(config.AdministratorEmail, config.AdministratorEmail);
            mailMessage.Subject = "Test Email";
            mailMessage.Body = "This is a Test email, please not reply";
            mailMessage.IsBodyHtml = true;
            GetSmtpClient().Send(mailMessage);
        }
    }
}
